using DTFramework;
using GameFramework.DataTable;
using System;
using System.Collections.Generic;
using UnityGameFramework.Runtime;
using GameEntry = DTFramework.GameEntry;

public static class DataTableExtension
{
    public static void LoadDataTable(this DataTableComponent dataTableComponent, string dataTableName, string dataTableAssetName, object userData)
    {
        if (string.IsNullOrEmpty(dataTableName))
        {
            Log.Warning("Data table name is invalid.");
            return;
        }
        string[] splitedNames = dataTableName.Split('_');
        if (splitedNames.Length > 2)
        {
            Log.Warning("Data table name is invalid.");
            return;
        }
        string dataRowClassName = "DR" + dataTableName;
        Type dataRowType = GameEntry.BuiltinData.GetType(dataRowClassName);
        if (dataRowType == null)
        {
            Log.Warning("Can not get data row type with class name '{0}'.", dataRowClassName);
            return;
        }
        string name = splitedNames.Length > 1 ? splitedNames[1] : null;
        DataTableBase dataTable = dataTableComponent.CreateDataTable(dataRowType, name);
        dataTable.ReadData(dataTableAssetName, AssetPriority.DataTableAsset, userData);
    }
}


